package service

import (
	"bloggo/structs"
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/logger"
	"gitee.com/chunanyong/zorm"
)

//SaveTbWebsiteConfigStruct 保存
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SaveTbWebsiteConfigStruct(ctx context.Context, tbwebsiteconfigstruct *structs.TbWebsiteConfigStruct) error {

	// tbwebsiteconfigstruct对象指针不能为空
	if tbwebsiteconfigstruct == nil {
		return errors.New("tbwebsiteconfigstruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSaveTbWebsiteConfigStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSaveTbWebsiteConfigStruct := zorm.Insert(ctx, tbwebsiteconfigstruct)

		if errSaveTbWebsiteConfigStruct != nil {
			return nil, errSaveTbWebsiteConfigStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSaveTbWebsiteConfigStruct != nil {
		errSaveTbWebsiteConfigStruct := fmt.Errorf("service.SaveTbWebsiteConfigStruct错误:%w", errSaveTbWebsiteConfigStruct)
		logger.Error(errSaveTbWebsiteConfigStruct)
		return errSaveTbWebsiteConfigStruct
	}

	return nil
}

//UpdateTbWebsiteConfigStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateTbWebsiteConfigStruct(ctx context.Context, tbwebsiteconfigstruct *structs.TbWebsiteConfigStruct) error {

	// tbwebsiteconfigstruct对象指针或主键Id不能为空
	if tbwebsiteconfigstruct == nil || (tbwebsiteconfigstruct.Id) < 1 {
		return errors.New("tbwebsiteconfigstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbWebsiteConfigStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbWebsiteConfigStruct := zorm.Update(ctx, tbwebsiteconfigstruct)

		if errUpdateTbWebsiteConfigStruct != nil {
			return nil, errUpdateTbWebsiteConfigStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbWebsiteConfigStruct != nil {
		errUpdateTbWebsiteConfigStruct := fmt.Errorf("service.UpdateTbWebsiteConfigStruct错误:%w", errUpdateTbWebsiteConfigStruct)
		logger.Error(errUpdateTbWebsiteConfigStruct)
		return errUpdateTbWebsiteConfigStruct
	}

	return nil
}

//UpdateNotZeroTbWebsiteConfigStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroTbWebsiteConfigStruct(ctx context.Context, tbwebsiteconfigstruct *structs.TbWebsiteConfigStruct) error {

	// tbwebsiteconfigstruct对象指针或主键Id不能为空
	if tbwebsiteconfigstruct == nil || (tbwebsiteconfigstruct.Id) < 1 {
		return errors.New("tbwebsiteconfigstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbWebsiteConfigStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbWebsiteConfigStruct := zorm.UpdateNotZeroValue(ctx, tbwebsiteconfigstruct)

		if errUpdateTbWebsiteConfigStruct != nil {
			return nil, errUpdateTbWebsiteConfigStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbWebsiteConfigStruct != nil {
		errUpdateTbWebsiteConfigStruct := fmt.Errorf("service.UpdateNotZeroTbWebsiteConfigStruct错误:%w", errUpdateTbWebsiteConfigStruct)
		logger.Error(errUpdateTbWebsiteConfigStruct)
		return errUpdateTbWebsiteConfigStruct
	}

	return nil
}

//DeleteTbWebsiteConfigStructById 根据Id删除
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeleteTbWebsiteConfigStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeleteTbWebsiteConfigStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.TbWebsiteConfigStructTableName).Append(" WHERE id=?", id)
		_, errDeleteTbWebsiteConfigStruct := zorm.UpdateFinder(ctx, finder)

		if errDeleteTbWebsiteConfigStruct != nil {
			return nil, errDeleteTbWebsiteConfigStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeleteTbWebsiteConfigStruct != nil {
		errDeleteTbWebsiteConfigStruct := fmt.Errorf("service.DeleteTbWebsiteConfigStruct错误:%w", errDeleteTbWebsiteConfigStruct)
		logger.Error(errDeleteTbWebsiteConfigStruct)
		return errDeleteTbWebsiteConfigStruct
	}

	return nil
}

//FindTbWebsiteConfigStructById 根据Id查询信息
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbWebsiteConfigStructById(ctx context.Context, id string) (*structs.TbWebsiteConfigStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.TbWebsiteConfigStructTableName).Append(" WHERE id=?", id)
	tbwebsiteconfigstruct := structs.TbWebsiteConfigStruct{}
	has, errFindTbWebsiteConfigStructById := zorm.QueryRow(ctx, finder, &tbwebsiteconfigstruct)

	//记录错误
	if errFindTbWebsiteConfigStructById != nil {
		errFindTbWebsiteConfigStructById := fmt.Errorf("service.FindTbWebsiteConfigStructById错误:%w", errFindTbWebsiteConfigStructById)
		logger.Error(errFindTbWebsiteConfigStructById)
		return nil, errFindTbWebsiteConfigStructById
	}

	if !has {
		return nil, nil
	}

	return &tbwebsiteconfigstruct, nil

}

//FindTbWebsiteConfigStructList 根据Finder查询列表
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbWebsiteConfigStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.TbWebsiteConfigStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	tbwebsiteconfigstructList := make([]structs.TbWebsiteConfigStruct, 0)
	errFindTbWebsiteConfigStructList := zorm.Query(ctx, finder, &tbwebsiteconfigstructList, page)

	//记录错误
	if errFindTbWebsiteConfigStructList != nil {
		errFindTbWebsiteConfigStructList := fmt.Errorf("service.FindTbWebsiteConfigStructList错误:%w", errFindTbWebsiteConfigStructList)
		logger.Error(errFindTbWebsiteConfigStructList)
		return nil, errFindTbWebsiteConfigStructList
	}

	return tbwebsiteconfigstructList, nil
}
